<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;
// use User\Api\UserApi;

/**
 * 云名片公共类
 */
class PublicController extends \Think\Controller {

    /* 第三方发送短信验证码接口 */
    public function sms_verify($receiver, $body){
        $post_data = array();
        $post_data['account'] = iconv('GB2312', 'GB2312', "daxuejia");
        $post_data['pswd'] = iconv('GB2312', 'GB2312', "Tch123456");
        $post_data['mobile'] = $receiver;
        $post_data['msg'] = mb_convert_encoding("$body", 'UTF-8', 'auto');
        $post_data['needstatus'] = 'true';
        $url = 'http://222.73.117.158/msg/HttpBatchSendSM';
        $o = "";
        foreach ($post_data as $k => $v) {
          $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);
        $url = $url . '?' . $post_data;

        $result = file_get_contents($url);
        $result = str_replace("\n", ',', $result);
        $msgid = explode(',', $result);
        //        $msgid = (string)$msgid[2];
        return $msgid[1];
    }

    /**
     * 根据session_id查用户id
     * @param string $session_id
     * @return 参数格式不对返回false，成功返回请求结果
     */
    public function get_userId($session_id)
    {
        if (empty($session_id)){
            return [];
        }
        //查用户id
        $condition = array('session_id'=>$session_id);
        $field = 'id';
        $CardMember = D('CardMember');
        $res = $CardMember->getMemberInfo($condition,$field);
        if($res === false){
            return false;
        }elseif(empty($res)){
            return [];
        }else{
            return $res['id'];
        }
    }
    /**
     * 根据session_id查名片id
     * @return integer -1-没有登录 false-查询错误 []-没有名片
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    function getCardId($phpsid){

        if(empty($phpsid)){
            return -1;
        }

        //查名片id
        $user_id = $this->get_userId($phpsid);//查用户id
        if($user_id === false){
            return false;
        }elseif ($user_id === []) {
            return -1;
        }

        $res = D('CardId')->getCidInfo(array('user_id'=>$user_id),'cart_id');
        if($res === false){
            return false;
        }elseif(empty($res)){
            return [];
        }elseif(empty($res['cart_id'])){
            return [];
        }else{
            return $res['cart_id'];
        }
    }

    /**
     * 检测用户是否登录(返回给后端)
     * @return integer 0-未登录，1-登录了
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    function is_login($phpsid){

        if(empty($phpsid)){
            return 0;
        }
        //查session
        $TtcMember = D('CardMember');
        $condition = array('session_id'=>$phpsid);
        $field = 'session_id';
        $res = $TtcMember->getMemberInfo($condition,$field);

        if($res){
          return 1;
        }else{
          return 0;
        }
    }

    /**
     * 生成名片二维码 false-没有名片
     */
    public function qrurl($id) {
        // 需要云名片id
        // $id = 15;

        require_once API_PATH.'Other/qrcode/phpqrcode.php';
        //云名片信息
        $dao = M('User');
        $info = $dao->find($id);
        if (empty($info)) {
            return false;
        }

        $value = C('QRC_PATH').$id; //二维码内容
        //容错级别
        $errorCorrectionLevel = 'M';//放logo时容错M，无logo可用L
        //生成图片大小
        $matrixPointSize = 11;
        //保存图片路径
	    $new_file =C('CARD_QCODE_PATH').date('Y-m-d',time())."/";
        if(!file_exists($new_file))
        {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0700);
        }

        $path = $new_file.$id.'.png';


        $logo = empty($info['head_img']) ? false : 'http://'.$_SERVER['HTTP_HOST'].get_cover($info['head_img'], 'path');
        $QR = $path;
        //生成二维码图片
        \QRcode::png($value, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        //已经生成的原始二维码图 加头像
        if ($logo !== false) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            ini_set('memory_limit', '-1');
            if (imageistruecolor($logo)){
                imagetruecolortopalette($logo, false, 255);
            }
            $QR_width = imagesx($QR);
            //二维码图片宽度
            $QR_height = imagesy($QR);
            //二维码图片高度
            $logo_width = imagesx($logo);
            //logo图片宽度
            $logo_height = imagesy($logo);
            //logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            //输出图片
            // Header("Content-type: image/png");
            // ImagePng($QR);
            ImagePng($QR,$path);
            return $path;
        }else{
            return $path;
        }
    }

    /**
     * 生成名片二维码 名片图加多一个参数
     */
    public function qrurlCardPic($id) {
        // 需要云名片id
        // $id = 15;

        require_once API_PATH.'Other/qrcode/phpqrcode.php';
        //云名片信息
        $dao = M('User');
        $info = $dao->find($id);
        if (empty($info)) {
            return false;
        }

        $value = C('QRC_PATH').$id.'?is_pic=1'; //二维码内容
        
        //容错级别
        $errorCorrectionLevel = 'M';//放logo时容错M，无logo可用L
        //生成图片大小
        $matrixPointSize = 11;
        //保存图片路径
        $new_file =C('CARD_QCODE_PATH').date('Y-m-d',time())."/";
        if(!file_exists($new_file))
        {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0700);
        }

        $path = $new_file.$id.'temp.png';


        $logo = empty($info['head_img']) ? false : 'http://'.$_SERVER['HTTP_HOST'].get_cover($info['head_img'], 'path');
        $QR = $path;
        //生成二维码图片
        \QRcode::png($value, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        //已经生成的原始二维码图 加头像
        if ($logo !== false) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            ini_set('memory_limit', '-1');
            if (imageistruecolor($logo)){
                imagetruecolortopalette($logo, false, 255);
            }
            $QR_width = imagesx($QR);
            //二维码图片宽度
            $QR_height = imagesy($QR);
            //二维码图片高度
            $logo_width = imagesx($logo);
            //logo图片宽度
            $logo_height = imagesy($logo);
            //logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            //输出图片
            // Header("Content-type: image/png");
            // ImagePng($QR);
            ImagePng($QR,$path);
            return $path;
        }else{
            return $path;
        }
    }

    /**
     * 查询默认的贺卡模板
     */
    public function getDefaultCardtemp(){
        $res = D('CardTemplate')->getCidInfo(array('is_default'=>['EQ',1]));
        if(empty($res)){
            $res = D('CardTemplate')->getCidInfo();
            return $res;
        }else{
            return $res;
        }
    }
    /**
     * 查询默认的背景模板
     */
    public function getDefaultBacktemp(){
        $res = D('BackgroundColor')->getBackgInfo(array('is_default'=>['EQ',1]));
        if(empty($res)){
            $res = D('BackgroundColor')->getBackgInfo();
            return $res;
        }else{
            return $res;
        }
    }

    /**
     * 生成跳转到制作页二维码 $bid贺卡id
     */
    public function ChooseMakeqrurl($id,$bid,$user_id = '') {
        // 需要云名片id
        // $id = I('get.id', 0, 'intval');
        // $id = 7;

        require_once API_PATH.'Other/qrcode/phpqrcode.php';
        if(empty($id)){
            $value = C('CHOOSEMAKE').'/'.$bid.'/-1'; //没有名片的跳转链接
        }else{
            //云名片信息
            $dao = M('User');
            $info = $dao->find($id);
            if (empty($info)) {
                $value = C('CHOOSEMAKE').'/'.$bid.'/-1'; //没有名片的跳转链接
            }else{
                $value = C('CHOOSEMAKE').'/'.$bid.'/'.$id; //有名片的跳转链接
            }
            if(!empty($user_id)){
                $value = C('CHOOSEMAKE').'/'.$bid.'/'.$id.'?uid='.$user_id; //有名片的跳转链接
            }
        }

        //容错级别
        $errorCorrectionLevel = 'L';//放logo时容错M，无logo可用L
        //生成图片大小
        $matrixPointSize = 11;
        //保存图片路径
        $new_file =C('CARD_QCODE_PATH').date('Y-m-d',time())."/";
        if(!file_exists($new_file))
        {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0700);
        }
        if($id === -1){
            $path = $new_file.'0.make.png';
        }else{
            $path = $new_file.$id.'.make.png';
        }
        $QR = $path;
        //生成二维码图片
        \QRcode::png($value, $QR, $errorCorrectionLevel, $matrixPointSize, 2);
        
        // $QR = imagecreatefromstring(file_get_contents($QR));    
        
        //输出图片   
        // Header("Content-type: image/png");
        // imagepng($QR);    
        // imagedestroy($QR);

        imagepng($QR);  
        return $QR;

    }

}
